package algorithm;

public class DifferentLength {
    public int lengthOfLongestSubstring(String s) {
        String longgest = "";
        StringBuilder tempStr = new StringBuilder();
        char[] c = s.toCharArray();
        for (int i = 0; i < s.length(); i++) {
            Character a = c[i];
            if (!tempStr.toString().contains(a.toString())) {
                tempStr.append(a);
            } else {
                if (tempStr.length() > longgest.length()) {
                    longgest = tempStr.toString();
                }
                if (tempStr.indexOf(a.toString()) != tempStr.length() - 1) {
                    tempStr.delete(0, tempStr.indexOf(a.toString()) + 1);
                    tempStr.append(a);
                    continue;
                }
                tempStr = new StringBuilder(a.toString());
            }
        }
        if (tempStr.length() > longgest.length()) {
            longgest = tempStr.toString();
        }
        return longgest.length();
    }

    public static void main(String[] args) {
        DifferentLength length = new DifferentLength();
        System.out.println(length.lengthOfLongestSubstring("aab"));
//        System.out.println(length.lengthOfLongestSubstring("ohvhjdml"));
    }
}
